//
//  CustomerShareView.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/10.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CustomerShareView.h"

@interface CustomerShareView ()

@property (nonatomic, strong) void(^CancelAction)();
@property (nonatomic, strong) void(^OtherAction)(NSInteger index,CustomerShareView *shareView);
@property (nonatomic, strong) UIControl *bgView;
@property (nonatomic, strong) UIView *popView;
@property (nonatomic) BOOL isShow;
@property (nonatomic) BOOL isActive;

@end

@implementation CustomerShareView

static CustomerShareView *sharedCustomerShareView = nil;

+ (CustomerShareView *)sharedCustomerShareView
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedCustomerShareView = [[CustomerShareView alloc]init];
        sharedCustomerShareView.isShow = NO;
        sharedCustomerShareView.isActive = NO;
    });
    return sharedCustomerShareView;
}


- (instancetype)initWithTitle:(NSString *)title
            cancelButtonTitle:(NSString *)cancelButtonTitle
           cancelButtonAction:(void (^)())CancelAction
            otherButtonAction:(void (^)(NSInteger, CustomerShareView*shareView))OtherAction
{
    //    self = [[LCCustomerActionSheet alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    if (self.isShow)
    {
        return self;
    }
    self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.isShow = YES;
    
    self.CancelAction = CancelAction;
    self.OtherAction = OtherAction;
    
    self.bgView = [[UIControl alloc]initWithFrame:self.bounds];
    self.bgView.alpha = 0;
    [self addSubview:self.bgView];
    [self.bgView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown];
    
    UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-200, SCREEN_WIDTH, 200)];
    popView.backgroundColor = [LCCommon getColor:@"37383d"];
    [self addSubview:popView];
    self.popView = popView;
    
    if (cancelButtonTitle)
    {
        UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.popView.bounds) - 48, SCREEN_WIDTH, 48)];
        [cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
        [cancelButton setTitleColor:[LCCommon getColor:@"b1b1b1"] forState:UIControlStateNormal];
        [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:18.0f]];
        [cancelButton setBackgroundColor:[UIColor clearColor]];
        [cancelButton addTarget:self action:@selector(cancelBtnTap) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *separatorView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.popView.bounds) - 48 - 1/([UIScreen mainScreen].scale), SCREEN_WIDTH, 1/([UIScreen mainScreen].scale))];
        separatorView.backgroundColor = [LCCommon getColor:@"454545"];
        
        [popView addSubview:cancelButton];
        [popView addSubview:separatorView];
        
       
    }
    
    for (int i=0; i < 4; i++)
    {
        UIButton *otherBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 59, 48, 48)];
        otherBtn.tag = i;
        [otherBtn setImage:[UIImage imageNamed:i==0?@"Share_QQ":(i==1?@"Share_QQZone":(i==2?@"Share_Weixin":@"Share_WeixinTimeLine"))] forState:UIControlStateNormal];
        [otherBtn addTarget:self action:@selector(otherButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        
        CGRect frame = otherBtn.frame;
        frame.origin.x = 25+i*(48+(SCREEN_WIDTH-50-48*4)/3);
        otherBtn.frame = frame;
        
        [popView addSubview:otherBtn];
        
         UILabel *textLable = [[UILabel alloc]initWithFrame:CGRectMake(otherBtn.x-10, otherBtn.y+otherBtn.height+8, 48+20, 14)];
        textLable.font = [UIFont systemFontOfSize:14.0];
        textLable.textColor = [LCCommon getColor:@"b1b1b1"];
        textLable.textAlignment = NSTextAlignmentCenter;
        textLable.text = i==0?@"QQ":(i==1?@"QQ空间":(i==2?@"微信好友":@"朋友圈"));
        [popView addSubview:textLable];
        
    }
    
    if(title)
    {
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 18, SCREEN_WIDTH, 18)];
        lable.text = title;
       
        lable.backgroundColor = [UIColor clearColor];
        lable.font = [UIFont systemFontOfSize:18.0f];
        lable.textColor = [LCCommon getColor:@"b1b1b1"];
        lable.textAlignment = NSTextAlignmentCenter;
        [popView addSubview:lable];
        
        
    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidChange:) name:UIApplicationWillTerminateNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidChange:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    
    return self;
}

- (void)showInView
{
    if (self.isActive)
    {
        return;
    }
    self.isActive = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }completion:^(BOOL finished) {
        self.bgView.backgroundColor = [UIColor blackColor];
        self.bgView.alpha = .4;
        
    }];
}

- (void)applicationDidChange:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [self removeFromSuperview];
    
}

- (void)dismiss
{
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = _popView.frame;
        frame.origin.y = SCREEN_HEIGHT;
    }completion:^(BOOL finished) {
        [[NSNotificationCenter defaultCenter]removeObserver:self];
        [self removeFromSuperview];
        
    }];
}

- (void)removeFromSuperview
{
    [super removeFromSuperview];
    [self removeSubviews];
}

- (void)removeSubviews
{
    for (id obj in self.subviews)
    {
        [obj removeFromSuperview];
    }
    self.isShow = NO;
    self.isActive = NO;
    self.CancelAction = nil;
    self.OtherAction = nil;
    self.bgView = nil;
    self.popView = nil;
    
}

- (void)cancelBtnTap
{
    if (self.CancelAction)
    {
        self.CancelAction();
    }
    [self dismiss];
}

- (void)otherButtonTap:(UIButton *)sender
{
    self.OtherAction(sender.tag,self);
}


@end
